package com.ttscm.srds.robot.seer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttscm.srds.config.RobotParam;
import com.ttscm.srds.datagram.SeerDataPacket;
import com.ttscm.srds.utils.ConnectionUtil;
import com.ttscm.srds.utils.PacketUtil2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version:
 * @author:
 * @className:
 * @packageName:
 * @description: 机器人配置 API
 * @date: 2022/07/22
 */
@Component
public class RobotConfig {
    private final static int ROBOT_PORT = 19207;

    private final static int API_LOCK_4005 = 4005;

    @Autowired
    private RobotParam config;

    /**
     * 抢占控制权
     *
     * @param nickName
     * @return
     * @throws IOException
     */
    public SeerDataPacket lock4005(String nickName) throws IOException {
        String nickName2 = nickName;
        if (!StringUtils.hasText(nickName2)) {
            nickName2 = "ttscm-srds";
        }

        Map<String, Object> bodyDataArea = new HashMap<>();
        bodyDataArea.put("nick_name", nickName2);
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_LOCK_4005, new ObjectMapper().writeValueAsString(bodyDataArea));

        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }
}
